<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends MY_Luckydog
{
	public function __construct()
	{
		parent::__construct();
        $this->load->model('luckydog/Account_model', 'account');
        $this->load->model('luckydog/Role_model', 'role');
        $login_status = $this->login_status = $this->check_login_status();
        if (empty($login_status)){
            redirect('/luckydog/login');
        }
        
	}
    public function accountList()
    {
        $_SESSION['v_menu'] = 'account';
        $account_list = $this->account->getAccountList();
        $data = array(
            'account_list'=>$account_list
        );

        $this->load->view('/luckydog/systemoption/account_list',$data);
    }
    //跳转至账号编辑页面
    public function toSetAccount(){
        $id = $this->input->get("id",true) ? $this->input->get("id",true) : 0;
        $role_list = $this->role->getRolelList();
        $role_name_list = array();
        foreach($role_list as $row){
            $role_name_list[$row['id']] = $row['name'];
        }


        $data = array("role_name_list"=>$role_name_list);
        if(!empty($id)){
            $this->load->model("luckydog/admin_user_model","user");
            $powerInfo = $this->role->getPowerInfo($id);
            $userInfo = $this->user->get_userinfo($id);
            $data['powerInfo'] = json_decode($powerInfo['power'],true);
            $data['groupID'] = $id;
            $data['userInfo'] = $userInfo;
            $data['id'] = $id;
        }
        $this->load->view('/luckydog/systemoption/set_account',$data);
    }

    /**
     * 编辑管理员信息
     */
    public function edit_account()
    {
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $username = $this->input->post('username', TRUE);
        $email = $this->input->post('email', TRUE);
        $roleid = $this->input->post('roleid',TRUE);
        $this->load->helper('email');
        $password = $this->input->post('password', TRUE);
        if(!valid_email($email))
        {
            echo json_encode(array('code'=>-1,'message'=>'邮箱格式不正确!'));
            return;
        }
        if(!empty($password) && !preg_match("/^[a-z0-9_]*$/i",$password))
        {
            echo json_encode(array('code'=>-3,'message'=>'密码只能是字母数字或下划线格式的字符!'));
            return;
        }
        if(!empty($password) && !preg_match("/^[a-z0-9_]{6,16}$/i",$password))
        {
            echo json_encode(array('code'=>-3,'message'=>'密码请设置为6-16个字符!'));
            return;
        }

            $check_username = $this->account->check_username($id,$username);
            if(!empty($check_username))
            {
                echo json_encode(array('code'=>-1,'message'=>'该姓名以被占用！'));
                exit;
            }

            $check_email = $this->account->check_email($id,$email);
            if(!empty($check_email))
            {
                echo json_encode(array('code'=>-1,'message'=>'该邮箱以被占用！'));exit;
            }

            if (!empty($password)) {
                $data['password'] = md6($password);
            }

            $data['username'] = $this->input->post('username',TRUE);
            $data['roleid'] = $roleid;
            $data['email'] = $email;
            $data['addtime'] = time();
            $data['operationname'] = $_SESSION['bangpai_admin_username'];

            $res = $this->account->edit_account($id, $data);
            echo json_encode(array('code'=>1));

    }

    //删除账号
    public function delAccount(){
        $id = $this->input->post("id",true);
        $res = $this->account->delAccountbyid($id);
        echo $res;
    }
    //设置角色启用状态
    public function setStatus(){
        $id = $this->input->post("id",true);
        $status = $this->input->post("status",true) ? $this->input->post("status",true) :-1;
        $res = $this->account->setStatus($id,$status);
        echo $res;
    }

}
/* End of file index.php */
/* Location: ./application/controllers/luckydog/role.php */